import { calculateCenter } from "@/utils/index.js";

export const drawPolygon = (mapData, map) => {
    // 处理数据
    mapData.map(item => {
        let pathArr = item.pathArr
        let color = item.color
        let name = item.name
        draw(pathArr, color, name)
    })
    // 绘制多边形
    function draw(pathArr, color, name) {
        const polygon = new AMap.Polygon({
            path: pathArr,
            fillColor: color, // 多边形填充颜色
            // fillOpacity: 0.6, // 填充色透明度
            strokeColor: "red", // 线条颜色
            strokeOpacity: 0, // 线条透明度
            strokeWeight: 1, // 线条粗细
            strokeStyle: 'solid',
            strokeDasharray: [5, 5],
        });
        map.add(polygon);

        // 文字标注
        if (name) {
            let position = calculateCenter(pathArr)
            // 创建一个 LabelMarker 实例
            var labelMarker = new AMap.LabelMarker({
                position: position,
                opacity: 1,
                zIndex: 2,
                text: {
                    content: name,
                    direction: 'center',
                    style: {
                        fontSize: 10,
                        fillColor: '#62718a',
                    }
                }
            });
            // 创建一个 LabelsLayer 实例来承载 LabelMarker，[LabelsLayer 文档](https://lbs.amap.com/api/jsapi-v2/documentation#labelslayer)
            var labelsLayer = new AMap.LabelsLayer({
                collision: true,
            });
            // 将 LabelMarker 实例添加到 LabelsLayer 上
            labelsLayer.add(labelMarker);
            // 将 LabelsLayer 添加到地图上
            map.add(labelsLayer);
        }
    }
}